package com.course.web.biz;

import com.course.domain.enums.Role;
import com.course.domain.model.Account;
import com.course.domain.model.Student;
import com.course.domain.service.UserService;
import com.course.web.model.StudentVM;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * Created by Justin on 2017/6/4.
 */
@Component
public class WebStudentBiz {

    private final UserService userService;

    @Autowired
    public WebStudentBiz(UserService userService) {
        this.userService = userService;
    }

    public StudentVM create(StudentVM vm) {
        Student student = userService.createStudent(vm.getUsername(), vm.getPassword(), toStudent(vm));
        vm.setStudentId(student.getId());
        vm.setAccountId(student.getAccountId());
        return vm;
    }

    public StudentVM update(StudentVM vm) {
        userService.updateAccount(toAccount(vm));
        userService.updateStudent(toStudent(vm));
        return vm;
    }

    public StudentVM delete(StudentVM vm) {
        userService.deleteStudent(toStudent(vm));
        return vm;
    }

    public StudentVM load(int studentId) {
        Student student = userService.loadStudent(studentId);
        Account account = userService.loadAccount(student.getAccountId());
        return transform(student, Collections.singletonMap(account.getId(), account));
    }

    public List<StudentVM> list() {
        List<Student> studentList = userService.listStudent();
        List<Integer> accountIdList = Lists.transform(studentList, Student::getAccountId);
        Map<Integer, Account> accountMap = Maps.uniqueIndex(userService.findAccount(accountIdList), Account::getId);
        return Lists.transform(studentList, student -> transform(student, accountMap));
    }

    private StudentVM transform(Student student,
                                Map<Integer, Account> accountMap) {
        Account account = accountMap.get(student.getAccountId());
        StudentVM vm = new StudentVM();
        vm.setStudentId(student.getId());
        vm.setAccountId(account.getId());
        vm.setUsername(account.getUsername());
        vm.setPassword(account.getPassword());
        vm.setName(student.getName());
        vm.setLink(student.getLink());
        vm.setAudio(student.getAudio());
        vm.setIntro(student.getIntro());
        vm.setCourseId(student.getCourseId());
        vm.setScore(student.getScore());
        vm.setExchange(student.getExchange());
        return vm;
    }

    private Account toAccount(StudentVM vm) {
        Account account = new Account();
        account.setId(vm.getAccountId());
        account.setRole(Role.STUDENT);
        account.setUsername(vm.getUsername());
        account.setPassword(vm.getPassword());
        account.setOAuthWx("");
        account.setStatus(1);
        return account;
    }

    private Student toStudent(StudentVM vm) {
        Student student = new Student();
        student.setId(vm.getStudentId());
        student.setAccountId(vm.getAccountId());
        student.setName(vm.getName());
        student.setLink(vm.getLink());
        student.setAudio(vm.getAudio());
        student.setIntro(vm.getIntro());
        student.setCourseId(vm.getCourseId());
        student.setScore(vm.getScore());
        student.setExchange(vm.getExchange());
        student.setStatus(1);
        return student;
    }
}
